package com.hgkj.cloud.base.service;

import com.hgkj.cloud.base.common.ResponseData;
import com.hgkj.cloud.base.domain.AdminUserChannel;
import com.hgkj.cloud.base.domain.EUser;
import com.hgkj.cloud.base.domain.TenantFrameworks;
import com.hgkj.cloud.base.util.ResponseUtil;
import com.hgkj.cloud.base.dao.AdminUserChannelMapper;
import com.hgkj.cloud.base.dao.EUserMapper;
import com.hgkj.cloud.base.dao.TenantFrameworksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AdminUserChannelService {
    @Autowired
    private AdminUserChannelMapper adminUserChannelMapper;
    @Autowired
    private EUserMapper eUserMapper;
    @Autowired
    private TenantFrameworksMapper tenantFrameworksMapper;

    public int deleteByPrimaryKey(Integer id) {
        return adminUserChannelMapper.deleteByPrimaryKey(id);
    }

    public int insert(AdminUserChannel record) {
        EUser eUser = eUserMapper.getUserInfoByOnlyUuid(record.getUuid());
        if (record.getChannelId().equals(eUser.getChannel_id())) {
            return 1;
        }
        if (adminUserChannelMapper.exists(record)) {
            return 1;
        }
        return adminUserChannelMapper.insert(record);
    }

    public int insertSelective(AdminUserChannel record) {
        return adminUserChannelMapper.insertSelective(record);
    }

    public AdminUserChannel selectByPrimaryKey(Integer id) {
        return adminUserChannelMapper.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(AdminUserChannel record) {
        return adminUserChannelMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(AdminUserChannel record) {
        return adminUserChannelMapper.updateByPrimaryKey(record);
    }

    public List<AdminUserChannel> listByUuid(String uuid) {
        return adminUserChannelMapper.listByUuid(uuid);
    }

    public int insertAll(String uuid, String channelIds) {
        int c = adminUserChannelMapper.deleteByUuid(uuid);
        if (StringUtils.isEmpty(channelIds) || channelIds.equals(",")) {
            return c;
        }
        c = 0;
        for (String channelId : channelIds.split(",")) {
            if (!channelId.equals("")) {
                AdminUserChannel adminUserChannel = new AdminUserChannel();
                adminUserChannel.setUuid(uuid);
                adminUserChannel.setChannelId(channelId);
                c += adminUserChannelMapper.insert(adminUserChannel);
            }
        }
        return c;
    }

    public ResponseData setSelfChannelManager(String user_uuid, String framework_ids, int set_channel_manager) {
        Map<String, Object> map = new HashMap<>();
        map.put("uuid", user_uuid);
        EUser eUser = eUserMapper.getUserInfo(map);
        if (StringUtils.isEmpty(eUser.getChannel_id())) {
            return ResponseUtil.getResponseData(-1, "操作失败：没有所属用户专属码");
        }
        map.clear();
        map.put("uuid", user_uuid);
        map.put("channel_id", eUser.getChannel_id());
        int c = adminUserChannelMapper.deleteByMap(map);
        map.clear();
        map.put("deleteNum", c);
        if (set_channel_manager == 1) {
            c = 0;
            if (framework_ids !=null && framework_ids.length() > 0) {
                List<TenantFrameworks> list = filterChildFrameworks(framework_ids);
                for (TenantFrameworks tenantFrameworks1 : list) {
                    AdminUserChannel adminUserChannel = new AdminUserChannel();
                    adminUserChannel.setUuid(user_uuid);
                    adminUserChannel.setChannelId(eUser.getChannel_id());
                    adminUserChannel.setFrameworkId(tenantFrameworks1.getId());
                    if (!adminUserChannelMapper.exists(adminUserChannel)) {
                        c += adminUserChannelMapper.insert(adminUserChannel);
                    }
                }
            } else {
                AdminUserChannel adminUserChannel = new AdminUserChannel();
                adminUserChannel.setUuid(user_uuid);
                adminUserChannel.setChannelId(eUser.getChannel_id());
                c += adminUserChannelMapper.insert(adminUserChannel);
            }
            map.put("addNum", c);
        }
        return ResponseUtil.getResponseData(map);
    }

    private List<TenantFrameworks> filterChildFrameworks(String framework_ids) {
        List<TenantFrameworks> list = new ArrayList<>();
        for (String fid : framework_ids.split(",")) {
            if (fid.equals("")) {
                continue;
            }
            TenantFrameworks tenantFrameworks = tenantFrameworksMapper.getFrameworkById(Long.parseLong(fid));
            list = addListByFilterPath(list, tenantFrameworks);
        }
        return list;
    }

    private List<TenantFrameworks> addListByFilterPath(List<TenantFrameworks> list, TenantFrameworks tenantFrameworks) {
        if (tenantFrameworks == null) {
            return list;
        }
        if (list.size() == 0) {
            list.add(tenantFrameworks);
            return list;
        }
        Long existChildId = null;
        for (TenantFrameworks tenantFrameworks1 : list) {
            // 是否有子部门在里面，有的话需要删除
            if (tenantFrameworks1.getTree_path().contains(tenantFrameworks.getTree_path())) {
                existChildId = tenantFrameworks1.getId();
                break;
            }
            // 已经有父级在里面，则返回
            if (tenantFrameworks.getTree_path().contains(tenantFrameworks1.getTree_path())) {
                return list;
            }
        }
        // 删除子部门
        if (existChildId != null) {
            final long fid = existChildId;
            list = list.stream().filter(t -> t.getId() != fid).collect(Collectors.toList());
        }
        list.add(tenantFrameworks);
        return list;
    }
}